<%-- 
    Document   : editMessage
    Created on : 05.11.2011, 17:23:04
    Author     : Дима
--%>

<%@page import="ru.ssau.utils.XMLHelper"%>
<%@page import="ru.ssau.lib.StringIntegerPair"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Calendar"%>
<%@page import="org.jdom.Element"%>
<%@page import="ru.ssau.entities.Message"%>
<%@page import="ru.ssau.server.Constants"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>   <%!
        private StringIntegerPair[] getStringIntegerPairList(HttpSession session, String parameter) throws java.io.IOException {

            String userListXMLString = (String) session.getAttribute(parameter);
            if (userListXMLString != null) {
                Element userListElement = XMLHelper.parseString(userListXMLString);
                StringIntegerPair[] values =
                        XMLHelper.loadStringIntegerPairs(userListElement.getChild(Constants.VALUES_XML_TAG));
                return values;
            } else {
                return new StringIntegerPair[0];
            }
        } %>

        <%
        String messageXML = (String) session.getAttribute(Constants.MESSAGE);
        Message message = null;
        String caption = ""; int userId = 0; int threadId = 0;
        String text = ""; String year = ""; String month = ""; String day = "";
        String hours = ""; String minutes = ""; String seconds = "";
        if(messageXML != null) {
            Element userElement = XMLHelper.parseString(messageXML);
            message = new Message();
            message.loadFromXMLElement(userElement);
            Calendar c = new GregorianCalendar();
            c.setTime(message.getPostDate());
            year = String.valueOf(c.get(Calendar.YEAR));
            month = String.valueOf(c.get(Calendar.MONTH) + 1);
            day = String.valueOf(c.get(Calendar.DAY_OF_MONTH));
            hours = String.valueOf(c.get(Calendar.HOUR));
            minutes = String.valueOf(c.get(Calendar.MINUTE));
            seconds = String.valueOf(c.get(Calendar.SECOND));
            caption = message.getCaption(); text = message.getText();
            userId = message.getUserId(); threadId = message.getThreadId();
            session.setAttribute(Constants.ID, message.getId());
        } %>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add/edit message</title>

        <style type="text/css">
            h4 {
                margin-top: 6px;
                margin-bottom: 3px;
            }
        </style>
    </head>
    <body>
        <h4>Enter data:</h4>
        <form name="messageSetForm" action="request" method="post">
            Caption: <input type="text" name="<%=Constants.CAPTION%>" value="<%=caption%>" size="10"/>
            <table>
                <tr>
                    <td align="right">User: </td>
                    <td>   <%
                      StringIntegerPair[] users;
                      users = getStringIntegerPairList(session, Constants.USER_LIST);
                      if(users != null) { %>
                        <select id="userList" name="<%=Constants.USER_ID_IN_MESSAGE%>">   <%
                          for(int i = 0; i < users.length; i++) { %>
                          <option <%=(users[i].getNumber()==userId)?"selected":""%> value="<%=users[i].getNumber()%>"> <%=users[i]%> </option>   <%
                          } %>
                        </select>   <%
                      } %>
                    </td>
                    <td align="right">Thread: </td>
                    <td>   <%
                      StringIntegerPair[] threads;
                      threads = getStringIntegerPairList(session, Constants.THREAD_LIST);
                      if(threads != null) { %>
                        <select id="threadList" name="<%=Constants.THREAD_ID_IN_MESSAGE %>">   <%
                          for(int i = 0; i < threads.length; i++) { %>
                            <option <%=(threads[i].getNumber()==threadId)?"selected":""%> value="<%=threads[i].getNumber()%>"> <%=threads[i]%> </option>   <%
                          } %>
                        </select>   <%
                      } %>
                    </td>
                </tr>
            </table>

            <h4>Post date:</h4>
            <input type="checkbox" <%=(messageXML==null)?"checked":""%> name="<%=Constants.DATE_SET_OPTION%>" value="<%=Constants.CURRENT_DATE%>"/>
            Set current date <br>
            <table>
                <tr>
                    <td align="center">Year</td>
                    <td align="center">Month</td>
                    <td align="center">Day</td>
                    <td align="center">Hours</td>
                    <td align="center">Minutes</td>
                    <td align="center">Seconds</td>
                </tr>
                <tr>
                    <td align="center"><input type="text" name="<%=Constants.YEAR%>" value="<%=year%>" size="5"/></td>
                    <td align="center"><input type="text" name="<%=Constants.MONTH%>" value="<%=month%>" size="5"/></td>
                    <td align="center"><input type="text" name="<%=Constants.DAY%>" value="<%=day%>" size="5"/></td>
                    <td align="center"><input type="text" name="<%=Constants.HOURS%>" value="<%=hours%>" size="5"/></td>
                    <td align="center"><input type="text" name="<%=Constants.MINUTES%>" value="<%=minutes%>" size="5"/></td>
                    <td align="center"><input type="text" name="<%=Constants.SECONDS%>" value="<%=seconds%>" size="5"/></td>
                </tr>
            </table>
            Text: <br>
            <textarea name="<%=Constants.TEXT%>" cols="25" rows="4" wrap="soft" style="margin-top: 2px; margin-bottom: 10px;"><%=text%></textarea>
            <br>   <%
              if(messageXML == null) { %>
                <button name="<%=Constants.ACTION%>" value="<%=Constants.DO_ADD_MESSAGE%>">Add message</button>   <%
              } else { %>
                <button name="<%=Constants.ACTION%>" value="<%=Constants.DO_EDIT_MESSAGE%>">Edit message</button>   <%
              } %>
        </form>
    </body>
</html>
